package mp3tagger.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import mp3model.Mp3Model;
import mp3tagger.Activator;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class Mp3Editor extends ViewPart implements PropertyChangeListener  {
	private Text text;
	private Mp3Model mp3;
	private Text text_1;
	private Text text_2;
	private Label lblNewLabel_1;
	private Label lblNewLabel_2;
	private Button btnSuggest;
	private Button btnNewButton;
	private Button btnNext;

	public Mp3Editor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(4, false));
		
		Label lblNewLabel = new Label(parent, SWT.NONE);
		lblNewLabel.setText("Filename");
		
		text = new Text(parent, SWT.BORDER);
		GridData gd_text = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text.widthHint = 210;
		text.setLayoutData(gd_text);
		new Label(parent, SWT.NONE);
		new Label(parent, SWT.NONE);
		
		lblNewLabel_1 = new Label(parent, SWT.NONE);
		lblNewLabel_1.setText("Artist");
		
		text_1 = new Text(parent, SWT.BORDER);
		GridData gd_text_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text_1.widthHint = 210;
		text_1.setLayoutData(gd_text_1);
		new Label(parent, SWT.NONE);
		new Label(parent, SWT.NONE);
		
		lblNewLabel_2 = new Label(parent, SWT.NONE);
		lblNewLabel_2.setText("Title");
		
		text_2 = new Text(parent, SWT.BORDER);
		GridData gd_text_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text_2.widthHint = 210;
		text_2.setLayoutData(gd_text_2);
		new Label(parent, SWT.NONE);
		new Label(parent, SWT.NONE);
		
		btnNext = new Button(parent, SWT.NONE);
		btnNext.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Activator.getDefault().getMp3ListController().previous();
			}
		});
		btnNext.setText("Previous");
		
		btnNewButton = new Button(parent, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Activator.getDefault().getMp3ListController().next();
			}
		});
		btnNewButton.setText("Next");
		
		btnSuggest = new Button(parent, SWT.NONE);
		btnSuggest.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				if ( mp3 != null) {
					String filename = mp3.getFilename();
					if (filename.contains(" - ")) {
						String[] splitted = filename.split(" - ");
						String artist = splitted[0];
						String title = splitted[1].substring(0, splitted[1].length() - 4);
						text_1.setText( artist);
						text_2.setText( title);
					}
				}
				
			}
		});
		btnSuggest.setText("Suggest");
		
		Button btnSave = new Button(parent, SWT.LEFT);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				mp3.setArtist(text_1.getText());
				mp3.setTitle( text_2.getText());
				mp3.save();
			}
		});
		btnSave.setText("Save");
		btnSave.setLayoutData( new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1,1));
		
		Activator.getDefault().getMp3ListController().addPropertyChangeListener( this);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
//		Activator.getDefault().getMp3ModelController().removePropertyChangeListener( this);
		super.dispose();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if ( evt.getNewValue() instanceof Mp3Model) {
				mp3 = (Mp3Model) evt.getNewValue();
				text.setText(mp3.getFilename());
				text_1.setText(mp3.getArtist());
				text_2.setText(mp3.getTitle());
		}
	}



}
